//---------------------------------------------------------------------------

#include "vcl_stl.h"
#pragma hdrstop

#include "h_base.h"
#include "h_xml.h"
#include "h_global.h"
using namespace h_base;
using namespace h_xml;

#include "h_g.h"
#include "h_stage.h"
#include "t_mainform.h"
#include "h_i18n.h"

#include "t_listform.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TListForm *ListForm;
//---------------------------------------------------------------------------
__fastcall TListForm::TListForm(TComponent* Owner)
   : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TListForm::Clear()
{
   JobMap.clear();
   JobList.clear();
   StringGrid1->RowCount = 2;
   StringGrid1->Rows[0]->Strings[0] = UTF8String(h_g::I18nString("t_listform:NAME"));
   StringGrid1->Rows[0]->Strings[1] = UTF8String(h_g::I18nString("t_listform:COMMAND"));

   for ( int i = 0; i < 2; i++ )
   {
      StringGrid1->Rows[1]->Strings[i] = "";
   }
}

void __fastcall TListForm::Reload()
{
   JobMap.clear();
   MainForm->SaveToJobList(JobMap);
   AssignRows();
}

void __fastcall TListForm::AssignRows()
{
   map<UTF8String, HJobListInfo> NewJobList;

   Filter(NewJobList);

   bool AllFlag = NewJobList.size() != JobList.size();

   if ( NewJobList.empty() )
   {
      StringGrid1->RowCount = 2;
      for ( int i = 0; i < 2; i++ )
      {
         StringGrid1->Rows[1]->Strings[i] = "";
      }
   }
   else
   {
      StringGrid1->RowCount = NewJobList.size() + 1;
      int i;
      map<UTF8String, HJobListInfo>::iterator Iter;
      map<UTF8String, HJobListInfo>::iterator IterNew;
      for ( Iter = JobList.begin(), IterNew = NewJobList.begin(), i = 1;
            i <= NewJobList.size(); i++, IterNew++ )
      {
         const HJobListInfo &NewInfo = IterNew->second;
         if ( AllFlag )
         {
            AssignOneRow(i, NewInfo);
         }
         else
         {
            const HJobListInfo &Info = Iter->second;
            if ( Info.Name != NewInfo.Name ||
                 Info.Command != NewInfo.Command )
            {
               AssignOneRow(i, NewInfo);
            }
            Iter++;
         }
      }
   }

   JobList = NewJobList;
   UpdateButtons(StringGrid1->Selection.Top);
}

void __fastcall TListForm::AssignOneRow(int RowIdx, const HJobListInfo &Info)
{
   StringGrid1->Rows[RowIdx]->Strings[0] = Info.Name;
   StringGrid1->Rows[RowIdx]->Strings[1] = Info.Command;
}

void __fastcall TListForm::Filter(map<UTF8String, HJobListInfo> &NewJobList)
{
   UTF8String NameFilter = EditText->Text.Trim().UpperCase();

   NewJobList.clear();

   for ( map<UTF8String, HJobListInfo>::iterator Iter = JobMap.begin();
         Iter != JobMap.end(); Iter++ )
   {
      const HJobListInfo &Info = Iter->second;
      if ( NameFilter != "" && Info.Name.Pos(NameFilter) == 0 &&
           Info.CommandUpper.Pos(NameFilter) == 0 &&
           Info.TagUpper.Pos(NameFilter) == 0 )
      {
         continue;
      }
      NewJobList[Info.Name] = Info;
   }
}

void __fastcall TListForm::ButtonFilterClick(TObject *Sender)
{
   AssignRows();
}
//---------------------------------------------------------------------------

void __fastcall TListForm::UpdateButtons(int Row)
{
   UTF8String JobName = StringGrid1->Rows[Row]->Strings[0];
   HStage *Stage = MainForm->FindControlByName(JobName);

   if ( Stage == NULL )
   {
      ButtonGotoThisGroup->Enabled = false;
      return;
   }

   ButtonGotoThisGroup->Enabled = true;
}

void __fastcall TListForm::StringGrid1SelectCell(TObject *Sender, int ACol,
      int ARow, bool &CanSelect)
{
   UpdateButtons(ARow);
}
//---------------------------------------------------------------------------

void __fastcall TListForm::ButtonGotoThisGroupClick(TObject *Sender)
{
   UTF8String JobName = StringGrid1->Rows[StringGrid1->Selection.Top]->Strings[0];

   if ( JobName == "" )
   {
      return;
   }

   MainForm->GotoGroupAndFocusToJob(JobName);
   Close();
}
//---------------------------------------------------------------------------

